package cn.edu.scau.cmi.ema.controller;

import cn.edu.scau.cmi.ema.base.CmiPagination;
import cn.edu.scau.cmi.ema.base.CmiEmaController;
import cn.edu.scau.cmi.ema.domain.Unit;
import cn.edu.scau.cmi.ema.util.CmiHibernateProxyUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Set;

@Controller("UnitController")
public class UnitController extends CmiEmaController {

	@RequestMapping("/listUnits")
	public ModelAndView listUnits(HttpServletRequest request, @ModelAttribute CmiPagination pagination) {
		ModelAndView mav = new ModelAndView();

		pagination = unitPaginationService.updatePagination(request, pagination);
		Set<Unit> units = unitPaginationService.getPaginationUnits(pagination);

		mav.addObject("units", units);
		mav.addObject("entityName", "unit");
		mav.addObject("pagination", pagination);
		mav.setViewName("unit/listUnits");
		return mav;
	}

	public String listUnits() {
		return "redirect:/listUnits";
	}

	@RequestMapping("/viewUnit")
	public ModelAndView viewUnit(@RequestParam Integer unitId) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("unit", unitDAO.findUnitByPrimaryKey(unitId));
		mav.setViewName("unit/viewUnit");
		return mav;
	}

	@RequestMapping(value = "/confirmDeleteUnits")
	public ModelAndView confirmDeleteUnits(
			@RequestParam(value = "deleteUnitsIds", required = true) Set<Integer> deleteUnitsIds) {
		ModelAndView mav = new ModelAndView();

		Set<Unit> deleteUnits = unitDAO.findUnitsByIds(deleteUnitsIds);
		Set<Unit> referencedUnits = unitService.loadReferenceUnits(deleteUnits);
		boolean hasReferenced;
		if (referencedUnits.size() == 0) {
			mav.addObject("deleteEntitys", deleteUnits);
			hasReferenced = false;
		} else {
			deleteUnits.removeAll(referencedUnits);
			mav.addObject("referencedEntitys", referencedUnits);
			mav.addObject("deleteEntitys", deleteUnits);
			hasReferenced = true;
		}
		mav.addObject("entityName", "Unit");
		mav.addObject("hasReferenced", hasReferenced);
		mav.setViewName("forward:/listUnits");
		return mav;
	}

	@RequestMapping("/deleteUnit")
	public String deleteUnit(@RequestParam Integer unitId) {
		Unit unit = unitDAO.findUnitByPrimaryKey(unitId);
		unitService.deleteUnit(unit);
		return "forward:/listUnits";
	}

	@RequestMapping("/confirmDeleteUnit")
	public ModelAndView confirmDeleteUnit(@RequestParam Integer unitId) {

		ModelAndView mav = new ModelAndView();

		mav.addObject("unit", unitDAO.findUnitByPrimaryKey(unitId));

		mav.setViewName("unit/deleteUnit");

		return mav;
	}

	@RequestMapping("/deleteUnits")
	public String deleteUnits(@RequestParam(value = "deleteUnitsId", required = true) Set<Integer> deleteUnitsId) {
		unitService.deleteUnits(deleteUnitsId);
		return "forward:/listUnits";
	}

	@RequestMapping("/editUnit")
	public ModelAndView editUnit(@RequestParam Integer unitId) {

		ModelAndView mav = new ModelAndView();

		Unit unit = unitDAO.findUnitByPrimaryKey(unitId);
		// 将需要编辑的对象加到mav对象中
		mav.addObject("unit", unit);

		// 把所有的引用unit对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(unitService.loadUnitObjectReferencedSet(unit));

		// 将编辑页面unit添加到mav中
		mav.setViewName("unit/editUnit");
		return mav;
	}

	@RequestMapping("/newUnit")
	public ModelAndView newUnit() {
		ModelAndView mav = new ModelAndView();
		Unit unit = new Unit();

		mav.addObject("unit", unit);

		// 把所有的引用unit对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(unitService.loadUnitObjectReferencedSet(unit));

		mav.addObject("newFlag", true);
		mav.setViewName("unit/editUnit");

		return mav;
	}

	@RequestMapping("/saveUnit")
	public ModelAndView saveUnit(@ModelAttribute Unit unit) {

		ModelAndView mav = new ModelAndView();

		Unit existingUnit = CmiHibernateProxyUtil
				.deproxy(unitDAO.findUnitByPhysicalPrimaryKey(unit.getName(), unit.getType()));

		// 新建记录
		if (unit.getId() == null) {
			if (unit.equals(existingUnit)) {

				mav.addAllObjects(unitService.loadUnitClassReferencedSet(unit));
				// mav = editContinue(mav, unit);
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("unit/editUnit");
			} else {
				unitService.saveUnit(unit);
				mav.setViewName("forward:/listUnits");
			}
		}
		// 编辑记录
		else {
			Unit originUnit = CmiHibernateProxyUtil.deproxy(unitDAO.findUnitByPrimaryKey(unit.getId()));
			if (!unit.equals(originUnit) && unit.equals(existingUnit)) {
				mav.addAllObjects(unitService.loadUnitClassReferencedSet(unit));
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("unit/editUnit");
			} else {
				unitService.saveUnit(unit);
				mav.setViewName("forward:/listUnits");
			}
		}
		return mav;
	}

	@RequestMapping("/unitController/binary.action")
	public ModelAndView streamBinary(@ModelAttribute HttpServletRequest request,
			@ModelAttribute HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("streamedBinaryContentView");
		return mav;

	}

}
